import { TimeoutManager } from "./TimeoutManager";

export class Tooler {
  static generateRandomId(): string {
    return Math.random().toString(36).substr(2, 6);
  }

  static sleep(ms: number): Promise<void> {
    return new Promise(resolve => TimeoutManager.setTimeout(resolve, ms));
  }

  static getRandomInt(a: number, b: number): number {
    return Math.floor(Math.random() * (b - a + 1) + a);
  }

  static groupArrayBySeven<T>(ary: T[]): T[][] {
    let result: T[][] = [];
    for (let i = 0; i < ary.length; i += 7) {
      result.push(ary.slice(i, i + 7));
    }
    return result;
  }

  static loadData(url: string): Promise<any> {
    const token = 'your_actual_token_here'; // 替换为你的实际 token
    return new Promise(resolve => {
      fetch(url, {
        headers: {
          'Authorization': `Basic ${token}`
        }
      })
        .then(response => response.json())
        .then(data => {
          resolve(data);
        })
        .catch(error => {
          console.error('There was a problem fetching the data:', error);
          resolve(null);
        });
    });
  }
}